import React, {Component} from 'react';
import {
	AppRegistry,
	Dimensions,
	Platform,
	NativeModules,
	DeviceInfo,
	StyleSheet,
	Image,
	Text,
	View,
	TextInput,
	PixelRatio,
	TouchableOpacity,
	ScrollView, Alert, AsyncStorage
} from 'react-native';

var Common = require('./common');
const pixelRatio = PixelRatio.get();
const dismissKeyboard = require('dismissKeyboard');
export default class ChangePassword extends Component<Props> {
	static navigationOptions = {
		headerTitle: '修改密码',
		tabBarLabel: '修改密码',

	};

	constructor(props) {
		super(props);
		this.state = {
			oldpassword: '',
			password: '',
			repassword: ''
		};
	}

	login() {
		var _this = this;
		Common.ajaxPost(Common.url + 'login.php', function (res) {
			if (res.code == "0000") {

			}
		}.bind(this), {verify: '08login', t: ''});
	}

	toRegister() {
		this.props.navigation.navigate('Register')
	}

	changepassword(){
		var _this = this;
		console.log(this.state.login_email);

		if(this.state.oldpassword == ''){
			Alert.alert('请填写旧密码');
			return false;
		}

		if(this.state.password == ''){
			Alert.alert('请填写新密码');
			return false;
		}

		if(this.state.repassword == ''){
			Alert.alert('请填写确认密码');
			return false;
		}

		var config = {
			method: 'POST',
			headers: {
				'Accept': 'application/json',
				'Content-Type': 'application/json',
				'credentials': 'include'
			},
			credentials: 'include'
		}
		var param = {verify: '08login', t: '', is_ajax: 1, oldpassword: this.state.oldpassword, password: this.state.password, repassword: this.state.repassword}
		if (param) {
			config["body"] = JSON.stringify(param);
		}

		fetch(Common.url + 'thinksns/index.php?app=public&mod=Account&act=doModifyPassword&is_ajax=1', config).then((response) =>{
			console.log(response.headers)
			return response.json()
		} ).then((res) => {
			if (res.status == 1) {
				console.log(res)
				Alert.alert(res.info)
				this.props.navigation.navigate('Mine');
			}
			else if(res.status == 401){
				this.props.navigation.navigate('Login');
			}
			else{
				Alert.alert(res.info)
			}
		})
	}

	componentDidMount() {
		// console.log(this.props.navigation.state.params)
		// if(this.props.navigation.state.params.from){
		//   this.setState({
		//     from:this.props.navigation.state.params,
		//   })
		// }
	}

	render() {
		const {navigate} = this.props.navigation;
		return (
			<View style={styles.container}>
				<View style={styles.row}>
					{/*<Image style={{width: 12, height: 17, marginRight: 20}} source={require('./images/iphone.png')}/>*/}
					<Image style={{width: 12, height: 17, marginRight: 20}} source={require('./images/mima.png')}/>
					<TextInput
						style={styles.text}
						onChangeText={(text) => this.setState({
							oldpassword: text
						})}
						placeholder='请输入旧密码'
						placeholderTextColor='#d9d9d9'
					/>
				</View>
				<View style={styles.row}>
					{/*<Image style={{width: 15, height: 11, marginRight: 20}}*/}
					       {/*source={require('./images/yanzhengma.png')}/>*/}
					<Image style={{width: 15, height: 11, marginRight: 20}}
					       source={require('./images/mima.png')}/>
					<TextInput
						style={styles.text}
						onChangeText={(text) => this.setState({
							password: text
						})}
						placeholder='请输入新密码'
						placeholderTextColor='#d9d9d9'
					/>
				</View>
				<View style={styles.row}>
					<Image style={{width: 13, height: 16, marginRight: 20}} source={require('./images/mima.png')}/>
					<TextInput
						style={styles.text}
						onChangeText={(text) => this.setState({
							repassword: text
						})}
						placeholder='请输入确认密码'
						placeholderTextColor='#d9d9d9'
					/>
					<TouchableOpacity
						activeOpacity={0.6}
					>
						<Image style={{width: 100, height: 40, marginRight: 30}} source={{uri: this.state.imgsrc}}/>
					</TouchableOpacity>
				</View>
				<TouchableOpacity style={styles.btnLogin} activeOpacity={0.6} onPress={this.changepassword.bind(this)}>
					<Text style={{color: '#fff'}}>确定</Text>
				</TouchableOpacity>
			</View>
		);
	}
}

const styles = StyleSheet.create({
	container: {
		flex: 1,
		height: Common.screenHeight,
		backgroundColor: '#fff',
	},
	row: {
		flexDirection: 'row',
		alignItems: 'center',
		paddingLeft: 36,
		borderBottomWidth: 1,
		borderBottomColor: '#f0f0f0'
	},
	text: {
		flex: 1,
		height: 54,
		color: '#333'
	},
	btnLogin: {
		height: 36,
		backgroundColor: '#3e79fe',
		marginRight: 38,
		marginLeft: 38,
		marginTop: 42,
		marginBottom: 38,
		borderRadius: 6,
		justifyContent: 'center',
		alignItems: 'center',
	},
	tips: {
		textAlign: 'center',
		fontSize: 14,
		color: '#a7a7a7'
	}
});
